package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import school.domain.Product;
import school.domain.User;
import school.mapper.ProductMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.pojo.ProductPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@RequestMapping("admin/product")
@Controller
public class AdminProductController extends BaseController {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 列表
     *
     * @param info
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(ProductPo info, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (info.getName() != null) {
            criteria.andCondition("name like '%" + info.getName() + "%'");
        }

        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ProductPo> pageInfo = new PageInfo<>(productMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));

        for (Product product : pageInfo.getList()) updateProduct(product);

        return "admin/product/list";
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Product product = productMapper.selectByPrimaryKey(id);
        model.addAttribute(product);
        return "admin/product/detail";
    }

    /**
     * 修改信息
     *
     * @param product
     * @return
     */
    @RequestMapping("update")
    public String update(Product product, MultipartFile icon) {
        if (!icon.isEmpty()) {
            String path = saveFile(icon);
            product.setImg(path);
        }
        productMapper.updateByPrimaryKeySelective(product);
        return refresh();
    }

    @GetMapping("add")
    public String add() {
        return "admin/product/add";
    }

    @PostMapping("add")
    public String add(Product product, MultipartFile icon) {
        if (!icon.isEmpty()) {
            String path = saveFile(icon);
            product.setImg(path);
        }
        product.setStatus("拍卖中");
        product.setCount(0);
        product.setCurrent(product.getPrice());
        product.setAddtime(new Date());
        productMapper.insertSelective(product);
        return "redirect:list";
    }

    /**
     * 完成
     *
     * @return
     */
    @RequestMapping("fin/{id}")
    public String fin(@PathVariable Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product.getIllegaluser() == null || product.getIllegaluser() == 0) {
            product.setStatus("流拍");
        } else {
            User user = userMapper.selectByPrimaryKey(product.getIllegaluser());
            user.setIllegal(user.getIllegal() + 1);
            userMapper.updateByPrimaryKeySelective(user);
            product.setStatus("完成");
        }
        productMapper.updateByPrimaryKeySelective(product);
        return refresh();
    }

    /**
     * 违约
     *
     * @param id
     * @return
     */
    @RequestMapping("wy/{id}")
    public String wy(@PathVariable Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product.getIllegaluser() == null || product.getIllegaluser() == 0) {
            product.setStatus("流拍");
        } else {
            User user = userMapper.selectByPrimaryKey(product.getIllegaluser());
            user.setDeal(user.getDeal()+1);
            userMapper.updateByPrimaryKeySelective(user);
            product.setStatus("违约");
        }
        productMapper.updateByPrimaryKeySelective(product);
        return refresh();
    }

}
